#pragma once
#include <vector>
#include <string>

template<typename A> 
inline void serialize(std::vector<byte>& vec, const A& arg) {
   arg.toVec(vec);
}

template<>
inline void serialize<int>(std::vector<byte>& vec, const int& arg) {
   int val = htonl(arg);
   vec.push_back((val) & 0xff);  
   vec.push_back((val >> 8) & 0xff);
   vec.push_back((val >> 16) & 0xff);
   vec.push_back((val >> 24) & 0xff);
}

template<>
inline void serialize<uint32_t>(std::vector<byte>& vec, const uint32_t& arg) {
   uint32_t val = htonl(arg);
   vec.push_back((val) & 0xff);  
   vec.push_back((val >> 8) & 0xff);
   vec.push_back((val >> 16) & 0xff);
   vec.push_back((val >> 24) & 0xff);
}

template<>
inline void serialize<short>(std::vector<byte>& vec, const short& arg) {
   short val = htons(arg);
   vec.push_back((val) & 0xff);  
   vec.push_back((val >> 8) & 0xff);
}

template<>
inline void serialize<unsigned short>(std::vector<byte>& vec, const unsigned short& arg) {
   unsigned short val = htons(arg);
   vec.push_back((val) & 0xff);  
   vec.push_back((val >> 8) & 0xff);
}

template<>
inline void serialize<bool>(std::vector<byte>& vec, const bool& arg) {
   vec.push_back((int)arg);  
}

template<>
inline void serialize<std::string>(std::vector<byte>& vec, const std::string& arg) {
   serialize<int>(vec, arg.length());
   vec.insert(vec.end(), arg.begin(), arg.end());
}

